package com.ag.score.thread.service;

import com.ag.common.tools.DateUtil;
import com.ag.score.content.ApplicationContextProvider;
import com.ag.score.entity.TConfig;
import com.ag.score.mapper.TConfigMapper;
import com.ag.score.service.global.ChannelSupervise;
import com.ag.score.util.ScoreParam;
import com.redis.RedisKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Random;

public class DataService {
    protected final static Logger logger = LoggerFactory.getLogger(DataService.class);

    protected TConfigMapper tConfigMapper = ApplicationContextProvider.getBean(TConfigMapper.class);

    //当前日期
    private String currentDate = "";
    private double startData = 0;

    /**
     * 生成数据
     */
    public void createData(){
        int hour = DateUtil.getHour();
        if(hour < ScoreParam.startHour || hour >= ScoreParam.endHour){
            startData = 0;
            logger.info("hour="+hour+",return");
            return ;
        }
        String time = DateUtil.date2String(new Date(),"HH:mm");
        if(time.equals(currentDate)){
            return;
        }
        currentDate = time;
        double data = getOldData();
        data = queryNewData(data);
        //存redis
        RedisKit.set(ScoreParam.dataRedisKey,data+"");
        RedisKit.set(time,data+"",10*60*60);
        if(startData == 0){
            startData = data;
            RedisKit.set(ScoreParam.dataStartRedisKey,data+"");
        }
        //入库
        TConfig dataConfig = tConfigMapper.selectById(ScoreParam.dataDbId);
        if(dataConfig == null){
            dataConfig.setId(ScoreParam.dataDbId);
            dataConfig.setNumval(data);
            tConfigMapper.insert(dataConfig);
        }else{
            dataConfig.setNumval(data);
            tConfigMapper.updateById(dataConfig);
        }
        logger.info("time="+time+",data="+data);
        //执行socket 群发
        ChannelSupervise.send2All("{\"time\":\""+time+"\",\"data\":"+data+"}");
    }

    /**
     * 生成新数据
     * @param data
     * @return
     */
    private double queryNewData(double data){

        Random random = new Random() ;
        int num = random.nextInt(4) ;
        random = new Random() ;
        if(random.nextInt(100)>50){
            num = num * -1;
        }
        if(num == 0){
            num = 1;
        }

        return data + num ;
    }


    /**
     * 获取上一次数据值
     * @return
     */
    private double getOldData(){
        String val = RedisKit.getString(ScoreParam.dataRedisKey);
        if(val !=null){
            try {
               return  new Double(val);
            }catch (Exception e){
                e.printStackTrace();
                logger.error("DataService.getOldData error="+e.getMessage());
            }
        }
        //从数据库取值
        TConfig dataConfig = tConfigMapper.selectById(ScoreParam.dataDbId);
        if(dataConfig == null){
            return 10000;
        }
        return dataConfig.getNumval();

    }


}
